// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        sc:cc.Node,
        itemHieght:0,
        offsetY:0,
        gidCountList:[],
    },


    init(sc){
        if(this.sc)
            return ;
        this.sc = sc;
        let scView = sc.getComponent(cc.ScrollView)
        let content = scView.content;
        for(let key in content.children)
        {
            let child = content.children[key];
            child.active = false;
        }
    },
    //显示数据 此种显示每次显示都需要重新刷新数据
    // sc scrollview
    // pre prefab
    // data 数据
    // cb 显示回调
    // gid groupID 
    showData(pre,data,cb,offsetY = 10,gid = -1){
        if(this.sc == null || data == null)
            return ;
        let scView = this.sc.getComponent(cc.ScrollView)
        let content = scView.content;
        content.height = data.length * (pre.height + offsetY);
        this.itemHieght = pre.height;
        this.offsetY = offsetY;
        let startIndex = 0;
        let endIndex = data.length
        if(gid == 0)
        {
            this.gidCountList[0] = data.length;
        }
        else if(gid == 1)
        {
            startIndex += this.gidCountList[0];
            endIndex += this.gidCountList[0];
        }
       for(let i = startIndex ; i < endIndex;++i)
       {
            let item = content.children[i];
            if(item == null || (item.gid != null && gid != item.gid))
            {
                item = cc.instantiate(pre);
                item.parent = content;
            }
            cb(item,data[i]);
            item.gid = gid;
            item.index = i;
            item.active = true;
            item.scale = 1;
            item.setPosition(cc.v2(0,-item.height/2-(offsetY+item.height)*i));
           
       }
       scView.scrollToTop(0.1,true);
       cc.log(content.children);
    },
    //通过gid显示数据
    showDataByGid(gid = 0){
        if(gid < -2)
            return ;
        let scView = this.sc.getComponent(cc.ScrollView)
        let content = scView.content;
        let count  = 0
        for(let i = 0;  i < content.childrenCount;++i)
        {
            let child = content.children[i];
            if(child.gid == gid)
            {
                child.active = true;
                child.setPosition(cc.v2(0,-this.itemHieght/2-(this.offsetY+this.itemHieght)*count));
                count++;
            }
            else
            {
                child.active = false;
            }

        }

        content.height = count*(this.itemHieght+this.offsetY);
    }

});
